<?php

namespace app\controller\admin\map;

use app\model\admin\calendar\CalendarConfig;
use app\model\admin\map\MapConfig;
use app\model\admin\map\MapGame;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class Config extends Backend
{
    protected $model;
    protected $noNeedAuth = ['index', 'info'];
    public function initialize()
    {
        $this->model = new MapConfig();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('game_id');
        $url = (new MapGame())->where("id",$id)->value("game_url");
        $info = $this->model->where("relation_id",$id)->find();
        if(!$info){
            $info = [
              "relation_id"=>$id,
              "title"=>"",
              "status"=>1,
              "opacity"=>"",
              "banner_img"=>"",
              "detail_banner_img"=>"",
            ];
        }else{
            $info = $info->toArray();
        }
        $info['game_url'] = $url;
        return $this->success('获取成功', $info);
    }


    //编辑
    public function edit()
    {
        $id   = $this->request->param('relation_id');
        $info = $this->model->where("relation_id",$id)->find();
        if (!$info) {
           $info = [];
        }else{
            $info = $info->toArray();
        }
        $post = CommonFun::filterPostData($this->request->post());
        if($post['opacity']>1||$post['opacity']<0){
            return $this->error('不透明度范围为0~1');
        }
        foreach ($post as $k => $v) {
            $info[$k] = $v;
        }
        try {
            if(isset($info['id'])){
                $updateRes = $this->model->update($info);
            }else{
                $updateRes = $this->model->save($info);
            }
            if ($updateRes) {
                $this->clearCache();
                return $this->success('编辑成功');
            } else {
                return $this->error('编辑失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }
}